<script setup>
import {onMounted, ref, reactive} from 'vue';
import {get, post} from '../js/http.js';
import taskModal from '../components/modal/task_modal.vue'
import icon from '../components/icon.vue'
import pageHead from '../components/pagehead.vue'
import nodata from '../components/nodata.vue'

const isEdit = ref(false)
let taskData = ref([])
let currentTask = ref({})
let indexData = ref([])
onMounted(() => {
  getTask();
  getIndex();
})

async function openModel(item) {
  isEdit.value = true
  currentTask.value = item
}
async function deleteHandle(item,itemIndex) {
  if(!item.id){
    return ;
  }
  post("/admin/task/delete",item).then(res => {
    if (res.success) {
      window.message("保存成功", 'success')
      getTask();
      taskData.value.splice(itemIndex,1);
    } else {
      window.message(res.message, 'warning')
    }
  }).catch(error => console.log(error))
}

function addHandle() {
  currentTask.value = {name: "NewTask",}
  isEdit.value = true
}

function runTask(task){
  window.confirm("确认是否立即运行", () => {}, () => {
    post("/admin/task/runTask", task).then(res => {
      if (res.success) {
        window.message("提交成功", 'success')
      } else {
        window.message(res.message, 'warning')
      }
    }).catch(error => console.log(error))
  })
}

async function getTask() {

  get("/admin/task/get").then(res => {
    taskData.value = res.result;
  }).catch(error => console.log(error))
}
async function getIndex() {
  get("/admin/setting/getSetting").then(res => {
    indexData.value = res.result.index
  }).catch(error => console.log(error))
}
async function closeModel() {
  isEdit.value = false

}
async function saveHandle() {
  isEdit.value = false
  let res = await post("/admin/task/update", currentTask.value)
  if (res.success) {
    window.message("保存成功", 'success')
    getTask();
  } else {
    window.message(res.message, 'warning')
  }
}
</script>
<template>
  <pageHead title="任务管理" iconName="calendar">
    <button class="button is-link is-outlined mr-3" @click="getTask"><icon icon="refresh" class="is-link mr-1"></icon>刷新</button>
    <button class="button is-link is-outlined" @click="addHandle"><icon icon="add-bold" class="is-link mr-1"></icon>新增任务</button>
  </pageHead>
  <div v-if="taskData && taskData.length > 0">
    <table class="table is-fullwidth">
      <thead>
      <tr>
        <th>名称</th>
        <th>类型</th>
        <th>索引</th>
        <th>时间</th>
        <th>语句</th>
        <th style="text-align: center">操作</th>
      </tr>
      </thead>
      <tbody>
      <tr v-for="(item,itemIndex) in  taskData" :key="item.name">
        <td>{{ item.name }}</td>
        <td :title="item.type">{{ item.type }}</td>
        <td>{{ item.indexName}}</td>
        <td>{{ item.cron }}</td>
        <td>{{ item.dbsql}}</td>
        <td style="text-align: center">
          <a @click="runTask(item)" class="mr-3">立即运行</a>
          <a @click="openModel(item)" class="mr-3">设置</a>
          <a @click="deleteHandle(item,itemIndex)">删除</a>
        </td>
      </tr>
      </tbody>
    </table>
  </div>
  <nodata v-else></nodata>
  <taskModal :currentTask="currentTask" :indexData="indexData"  @close="closeModel" @save="saveHandle" v-if="isEdit==true"></taskModal>

</template>
<style scoped>
</style>
